define([
  'jquery',
  'underscore',
  'backbone',
  'dateformatter',
  'models/feed/FeedModel',
  'collections/feeds/FeedsCollection',
  'views/feeds/FeedsListView',
  'text!templates/feeds/feedsTemplate.html',
  'views/appointments/PatientView', 
  'views/help/HelpView'
], function($, _, Backbone,Dateformatter, FeedModel, FeedsCollection, FeedsListView, feedsTemplate,PatientView,HelpView){

  var FeedsView = Backbone.View.extend({
    el: $(".mainpanel"),
    events: {'click .docappointment' : 'showdocview','click .patappointment' : 'showpatview','click .docappointmentprint' : 'showrddocview',
    	'click .patvideoappointment': 'startsession','click .labtest': 'showtest', 'click .patcareappointment':'showprecareview'} ,
    render: function(sideView){
    	this.$el.html(feedsTemplate);
        var feedsCollection = new FeedsCollection();
        var lOptions = {};
        var self=this;
        lOptions.success = function(){
        	if(feedsCollection.length>0){
            	var feedsListView = new FeedsListView({ collection: feedsCollection});
            	feedsListView.render(sideView);
            	self.feedsListView =feedsListView; 
        	}else{
        		var help = new HelpView();
        		help.render();
        	}

        };
        feedsCollection.fetch(lOptions);
    },
    showdocview: function(event){
    	 var element = event.currentTarget;
    	 
    	 this.selectedappointment = this.feedsListView.collection.get(element.id);
    	 
    	 if(this.selectedappointment.get('paymentStatus')){
    		 this.goTo("docappointment");
    	 }else{
	      		 $.msgBox({
					    title:"Appment",
					    content:"Patient has not made the payment.",
					    type:"error"
					});
    	 }
    	 
    	 
    },
    showrddocview: function(event){
   	 var element = event.currentTarget;
   	 
   	 this.selectedappointment = this.feedsListView.collection.get(element.id);
   	 this.goTo("docrdappointment");
   	 
   },
    showpatview: function(event){
  //      var patientView = new PatientView();
  //     patientView.render(event,true);
    	console.log(event.currentTarget.id);
    	window.open("/generatePrognosis/"+event.currentTarget.id,"_blank");
    },
    showprecareview: function(event){
    	console.log(event.currentTarget.id);
    	window.open("/getTemplate/"+event.currentTarget.id,"_blank");
    },
    startsession: function(event){
    	var element = event.currentTarget;
    	this.appointment = this.feedsListView.collection.get(element.id);

    	this.goTo('videosession');
    },
    showtest: function(event){
      	 var element = event.currentTarget;
       	 
       	 this.selectedappointment = this.feedsListView.collection.get(element.id);
       	 this.goTo("labtest");

    }
  });

  return FeedsView;
});
